package org.assistant.csp.domain;

import java.util.List;
import java.util.UUID;

import org.assistant.common.exception.GetFromDBException;
import org.assistant.common.exception.ImplementNotFoundException;
import org.assistant.common.exception.SaveIntoDBException;
import org.assistant.common.utils.ClassUtils;
import org.assistant.csp.dao.HomeDao;
import org.assistant.csp.dao.RelationUserDao;
import org.assistant.csp.dao.UserDao;
import org.assistant.csp.data.Home;
import org.assistant.csp.data.RelationUser;
import org.assistant.csp.data.UserSession;

public class UserDomainImpl implements UserDomain {
	private UserDao userDao;
	private HomeDao homeDao;
	private RelationUserDao relationUserDao;

	public UserDomainImpl() throws ImplementNotFoundException {
		this.userDao = (UserDao) ClassUtils
			.getInterfaceImplement(UserDao.class);
		this.homeDao = (HomeDao) ClassUtils
			.getInterfaceImplement(HomeDao.class);
		this.relationUserDao = (RelationUserDao) ClassUtils
			.getInterfaceImplement(RelationUserDao.class);
	}

	/**
	 * 判断登录账号是否在系统中已经存在
	 * 
	 * @param loginAccount
	 * @return
	 * @throws GetFromDBException
	 */
	public boolean isLoginAccountExist(String loginAccount)
			throws GetFromDBException {
		return userDao.isLoginAccountExist(loginAccount);
	}

	/**
	 * 判断email在系统中是否已经存在
	 * 
	 * @param email
	 * @return
	 * @throws GetFromDBException
	 */
	public boolean isEmailExist(String email) throws GetFromDBException {
		return userDao.isEmailExist(email);
	}

	/**
	 * 根据用户账号获取用户信息
	 * 
	 * @param loginAccount
	 * @return
	 * @throws GetFromDBException
	 */
	public UserSession getUserByLoginAccount(String loginAccount)
			throws GetFromDBException {
		return userDao.getUserByLoginAccount(loginAccount);
	}

	/**
	 * 判断密码是否正确
	 * 
	 * @param loginZh
	 * @param password
	 * @return
	 * @throws GetFromDBException
	 */
	public boolean isPasswordCorrect(String loginZh, String password)
			throws GetFromDBException {
		return userDao.isPasswordCorrect(loginZh, password);
	}

	/**
	 * 保存用户(注册)信息
	 * 
	 * @param userSession
	 *            -用户Session信息
	 * @param password
	 *            -密码
	 * @return UserSession
	 * @throws GetFromDBException
	 * @throws SaveIntoDBException
	 */
	public UserSession insertUser(UserSession userSession, String password)
			throws SaveIntoDBException, GetFromDBException {
		String homeId = UUID.randomUUID().toString();
		String userId = UUID.randomUUID().toString();

		// TODO 下面两句操作数据库的语句应该包含在事务中

		Home home = homeDao.insertHome(userId, homeId, userSession.getName());

		RelationUser relationUser = new RelationUser();
		relationUser.setId(userId);
		relationUser.setName(userSession.getName());
		relationUser.setHomeId(homeId);
		relationUserDao.insertRelationUser(relationUser);

		userSession.setId(userId);
		return userDao.insertUser(userSession, password, home);
	}

	/**
	 * 根据ID获取关联用户信息
	 * 
	 * @param relationUserId
	 * @return
	 * @throws GetFromDBException
	 */
	public RelationUser getRelationUserById(String relationUserId)
			throws GetFromDBException {
		return relationUserDao.getRelationUserById(relationUserId);
	}

	/**
	 * 根据家庭ID获取该家庭下的关联用户信息
	 * 
	 * @param homeId
	 * @return
	 * @throws GetFromDBException
	 */
	public List<RelationUser> getRelationUserByHome(String homeId)
			throws GetFromDBException {
		return relationUserDao.getRelationUserByHome(homeId);
	}

	/**
	 * 保存关联用户信息
	 * 
	 * @param relationUser
	 * @throws SaveIntoDBException
	 */
	public void insertRelationUser(String name, String homeId)
			throws SaveIntoDBException {
		RelationUser relationUser = new RelationUser();
		relationUser.setId(UUID.randomUUID().toString());
		relationUser.setName(name);
		relationUser.setHomeId(homeId);
		relationUserDao.insertRelationUser(relationUser);
	}
}
